package com.gdit.zax.servletjsp;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.SignDao;
import com.gdit.zax.dao.UserDao;
import com.gdit.zax.entitys.UserInfo;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.O;
import com.gdit.zax.utils.Repost;
import com.gdit.zax.utils.webUtil;

public class Signin extends HttpServlet {
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String token = webUtil.getCookie(request, "LoginCookie");
		
		Connection conn = DBUtil.getConnection();
		if (conn != null) {
			DAOFactory dao = DAOFactory.init(conn, request, response);
//			UserDao udao = dao.getUserDao().getInfo(Constant.LOGINTYPE_WEB, token).getId();
			UserDao udao = dao.getUserDao();
			SignDao sdao = dao.getSignDao();
			UserInfo info = udao.getInfo(Constant.LOGINTYPE_WEB, token);
			if (info!=null) {
				if (sdao.signin(info.getId())) {
					if (udao.addscore(info.getId(), Constant.SCORE_SIGNIN)) {
						Repost.message("签到成功！", request, response);
					} else {
						Repost.message( "签到失败，增加积分时出错，请联系管理员!", request, response);
					}
					
				} else {
					Repost.message("今日已签到，请改日再来吧！", request, response);
				}
			} else {
				Repost.nologin(response);
			}
		} else {
			Repost.dberror(request, response);
		}
	}

}
